home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_work.c < prev    next >
C/C++ Source or Header  |  1993-12-07  |  14KB  |  556 lines

  1. /****************************************************************************/
  2. /*                         DATABOSS MODULE: DB_WORK.C                       */
  3. /*           Set the editor TAB size to 3 for correct indentation           */
  4. /****************************************************************************/
  5.  
  6. #include "db_lsc.h"
  7.  
  8. #include <conio.h>
  9. #ifndef __TURBOC__
  10.     #include <graph.h>
  11.     #include <sys\types.h>
  12. #endif
  13. #include <io.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <sys\stat.h>
  19. #include <share.h>
  20. #include <errno.h>
  21. #include "db_types.h"
  22. #include "db_conio.h"
  23. #include "db_crit.h"
  24. #include "db_curs.h"
  25. #include "db_file.h"
  26. #include "db_funcs.h"
  27. #include "db_gvar.h"
  28. #include "db_heap.h"
  29. #include "db_key.h"
  30. #include "db_str.h"
  31. #include "db_win.h"
  32. #include "db_mnu.h"
  33. #include "db_util.h"
  34. #include "db_uwin.h"
  35. #include "db_work.h"
  36.  
  37. /****************************  GLOBAL VARIABLES  ****************************/
  38.  
  39. helpproc choosehelp;
  40. bool     permitesc;
  41.  
  42. /***************************  INTERNAL VARIABLES  ***************************/
  43.  
  44. static string  choices_;
  45. static byte    dispwid_,wwid_,whgt_,numc_,cnum_;
  46. static winptr  twinp_;
  47. static uchar   cmd_;
  48. static pathstr chkmask;
  49.  
  50. static bool initialized = False;
  51.  
  52. /*****************************  IMPLEMENTATION  *****************************/
  53.  
  54. void defChooseHelp(void)                              /* SN 3.5 */
  55. {
  56.    winptr tempWindow;                                 /* SN 3.5 */
  57.  
  58.    tempWindow = ChooseHelpWin();                      /* SN 3.5 */
  59.    disphelpwin(tempWindow);                           /* SN 3.5 */
  60.    closewin(&tempWindow);                             /* SN 3.5 */
  61.    return;                                            /* SN 3.5 */
  62. }
  63.  
  64.  
  65.  
  66. bool chkpickname(string instr)
  67. {
  68.     string ts;
  69.  
  70.     if (*chkmask == '\0') strcpy(chkmask,"*.*");
  71.     if (exitcode == F2) {
  72.         dirpick(ts,chkmask,DirIncl,DirExcl,0,0);
  73.         strcpy(instr,ts);
  74.         exitcode = ' ';
  75.     }
  76.     return(True);
  77. }
  78.  
  79. strptr removeparm(strptr sout, strptr wkstr, int p1)
  80. {
  81.     int x, c, sourceLength;
  82.  
  83.     sourceLength = strlen(wkstr);
  84.     if (*(wkstr + (p1+2)) == '(') {
  85.         c = 1;
  86.         x = p1 + 3;
  87.         while ((x < sourceLength) && (c > 0)) {
  88.             if (*(wkstr + x) == ')') c--;
  89.      else                 if (*(wkstr + x) == '(') c++;
  90.             if (c > 0) x++;
  91.         }
  92.         strcopy(sout,wkstr,p1+3,x-(p1+3));
  93.         strdelete(wkstr,p1+2,x-(p1+1));
  94.     }
  95.     else
  96.         *sout = '\0';
  97.     return(sout);
  98. }
  99.  
  100. bool replmacro(strptr wkstr)
  101. {
  102. /*
  103. Macro of the form :--
  104.   ¿<ID Letter>[(<Parameter>) ...]
  105.  
  106.   <ID Letter>                                         Parameter
  107.   ------------------------------------------------    -------------------------
  108.   @ -- Prompt for user input string                   User Prompt
  109.   C -- Choose box                                     CData and CList
  110.   D -- Directory listing                              File Mask
  111.   E -- returns environment variable                   Environment Variable Name
  112.   K -- returns -K if enhanced keyboard is off         NONE
  113.   L -- returns -LCD if Liquid Display switch is on    NONE
  114. */
  115.     uchar mode;
  116.     bool  fval;
  117.     int   p1, 
  118.      p2;
  119.     uchar ts[81],
  120.      ws1[81], 
  121.      ws2[81];
  122.     uchar tpic[4];
  123.  
  124.     strcpy(tpic, "80X");
  125.     tpic[2] = Pic_X; 
  126.     fval = True;  
  127.    p1 = strposch('¿',wkstr);
  128.     while (fval && (p1 >= 0)) {
  129.         mode = wkstr[p1+1];
  130.       ts[0] = '\0';
  131.         switch (mode) {
  132.             case 'L' : if (lcd) strcpy(ts,"-LCD"); break;
  133.             case 'K' : if (!ehk) strcpy(ts,"-K"); break;
  134.             case '@' :
  135.                 removeparm(ws1,wkstr,p1);
  136.         fval = replmacro(ws1);
  137.                 if (fval) {
  138.                     trim(ts,dbgetstr(ts,_Ch,ws1,"",tpic,"",nocheck,nohelp));
  139.                     fval = exitcode != QitKey;
  140.                 }
  141.                break;
  142.             case 'C' :
  143.                 removeparm(ws1,wkstr,p1);  
  144.         fval = replmacro(ws1);
  145.                 if (fval) {
  146.                     removeparm(ws2,wkstr,p1);  
  147.            if (ws2[0] == '\0') strcpy(ws2,ws1);
  148.                     fval = replmacro(ws2);
  149.                     if (fval) {
  150.                         fval = permitesc;  
  151.           permitesc = True;
  152.                         choose(ws1,ws1,ws2,ts);
  153.           permitesc = fval;
  154.                         fval = exitcode != QitKey;
  155.                     }
  156.                 }
  157.                break;
  158.             case 'D' :
  159.                 removeparm(ws1,wkstr,p1);
  160.                 fval = replmacro(ws1);
  161.                 if (fval) {
  162.                     removeparm(chkmask,wkstr,p1);
  163.                     fval = replmacro(chkmask);
  164.                     if (fval) {
  165.                         if (chkmask[0] == '\0') {
  166.              strcpy(chkmask,ws1);  
  167.              ws1[0] = '\0'; 
  168.           }
  169.                         if (ws1[0] != '\0')
  170.                             trim(ts,dbgetstr(ts,_Ch,ws1,"",tpic,"",chkpickname,nohelp));
  171.                         else
  172.                             dirpick(ts,chkmask,DirIncl,DirExcl,0,0);
  173.                         fval = (ts[0] != '\0');
  174.                     }
  175.                 }
  176.                break;
  177.             case 'E' :
  178.                 removeparm(ws1,wkstr,p1);
  179.                 fval = replmacro(ws1);
  180.                 if (fval) strcpy(ts,getenv(ws1));
  181.                break;
  182.         }  /* switch (mode) */
  183.         strinsert(ts,wkstr,p1+2);  strdelete(wkstr,p1,2);
  184.         p1 = strposch('¿',wkstr);
  185.     }
  186.     return(fval);
  187. }
  188.  
  189. /* SN 3.5 - the padout function is completely new for 3.5 */
  190.  
  191. uchar * padout(uchar *destination, uchar *theSource)
  192. {
  193.    int    arrayIndex, arraySize, longestElementSize;
  194.    uchar  array[25][80];
  195.    STRING localSource;
  196.    STRPTR source;
  197.    uchar  *sourceStart, *sourceEnd;
  198.    uchar  delimiter         = '|';
  199.    uchar  spaces            = ' ';
  200.    uchar  delimiterString[2];
  201.  
  202.    *destination = '\0';
  203.    strcpy(delimiterString,"|");
  204.    strcpy(localSource, theSource);
  205.    source = localSource;
  206.  
  207.    if (*source) {
  208.       if (*source == delimiter)                        source++;
  209.       if (*(source + strlen(source) - 1) != delimiter) strcat(source, delimiterString);
  210.  
  211.       sourceStart        = source;
  212.       sourceEnd          = source;
  213.       arrayIndex         = 0;
  214.       longestElementSize = 0;
  215.       while (sourceEnd) {
  216.           sourceEnd   = strchr(sourceStart, delimiter);
  217.           if (sourceEnd) {
  218.              mid(array[arrayIndex], sourceStart, 0, sourceEnd - sourceStart);
  219.              if (longestElementSize < strlen(array[arrayIndex])) longestElementSize = strlen(array[arrayIndex]);
  220.              sourceStart = ++sourceEnd;
  221.              arrayIndex++;
  222.           }
  223.       }
  224.  
  225.       arraySize = arrayIndex;
  226.       for (arrayIndex = 0; arrayIndex < arraySize; arrayIndex++) {
  227.           if (strlen(array[arrayIndex]) < longestElementSize) padAfter(array[arrayIndex], longestElementSize, spaces);
  228.           strcat(destination, array[arrayIndex]);
  229.           strcat(destination, delimiterString);
  230.       }
  231.       if (*(destination + strlen(destination) - 1) == delimiter) *(destination + strlen(destination) - 1) = '\0';
  232.    }
  233.  
  234.    return(destination);
  235. }
  236.  
  237.  
  238. uchar * padAfter(uchar *source, int size, uchar theCharacter)
  239. {
  240.    uchar *sourceStart = source;
  241.  
  242.    size = size - strlen(source);
  243.    source = source + strlen(source);
  244.    while (size--) *source++ = theCharacter;
  245.    *source = '\0';
  246.  
  247.    return(sourceStart);
  248. }
  249.  
  250. /* reserved for future use */
  251.  
  252. /*
  253. char * padBefore(char *source, int size, char theCharacter)
  254. {
  255. }
  256. */
  257.  
  258. bool choice(string clist, string fld)
  259. {
  260.     string ts,ts1;
  261.    string tempString;                                                /* SN 3.5 */
  262.  
  263.     strconcat(ts,"|",padout(tempString, clist),"|",NULL);             /* SN 3.5 */
  264.     strconcat(ts1,"|",fld,"|",NULL);
  265.     strupr(ts);
  266.     strupr(ts1);
  267.     return((bool) (strposstr(ts1,ts) != -1));
  268. }
  269.  
  270. strptr getchoice(strptr sout, string list, byte wid, byte cnum)
  271. {
  272.     string tmpclist;
  273.     byte i;
  274.  
  275.     strcpy(tmpclist,list);
  276.     for (i=1; i < cnum; i++)
  277.         strdelete(tmpclist,0,wid+1);
  278.     strcopy(sout,tmpclist,0,wid);
  279.     return(sout);
  280. }
  281.  
  282. void dispchoices(byte choice)
  283. {
  284.     string tmpclist,ts;
  285.     byte   i;
  286.  
  287.     strcpy(tmpclist,choices_);
  288.     for (i=1; i < choice; i++)
  289.         strdelete(tmpclist,0,dispwid_+1);
  290.     for (i=0; (i < whgt_) && ((choice+i) <= numc_); i++) {
  291.         writewxy(pad(ts,strcopy(ts,tmpclist,0,dispwid_),wwid_,Right),0,1,(byte)(i+1),twinp_);
  292.         strdelete(tmpclist,0,dispwid_+1);
  293.     }
  294. }
  295.  
  296. uchar firstchar(strptr s)
  297. {
  298.     while(*s && (*s == ' '))
  299.         s++;
  300.     return (*s);
  301. }
  302.  
  303. void setchoice(uchar ch)
  304. {
  305.     string tmpclist;
  306.     byte   i;
  307.  
  308.     strcpy(tmpclist,choices_);
  309.     i = 1;
  310.     while ((firstchar(tmpclist) != ch) && (tmpclist[0])) {
  311.         strdelete(tmpclist,0,dispwid_+1);
  312.         i++;
  313.     }
  314.     if (firstchar(tmpclist) == ch) {
  315.         cnum_ = i;
  316.         cmd_ = Enter;
  317.     }
  318.     else {
  319.         if ((ch != Esc) || !permitesc)
  320.             audible(Pip);
  321.     }
  322. }
  323.  
  324. strptr choose(strptr sout, string cdata, string clist, strptr fld)
  325. {
  326.    byte   tempNumber;
  327.    string tempString;
  328.  
  329.    chooseExact(tempString, cdata, clist, fld);
  330.    strcpy(sout,LSC_BaseError);
  331.    fldnum--;
  332.    return(sout);
  333. }
  334.  
  335.  
  336. byte chooseExact(strptr sout, string cdata, string clist, strptr fld)
  337. {
  338.     byte   x, y, line, datawid, tlen;
  339.     string ts;
  340.    string tempcdata, tempclist;
  341.  
  342.     line  = 1;
  343.     cnum_ = 1;
  344.    padout(tempcdata, cdata);
  345.    padout(tempclist, clist);
  346.  
  347.     strcpy(choices_,(tempclist[0] == '\0') ? tempcdata : tempclist);
  348.     if (choices_[0] == '|')
  349.         strcpy(choices_,&choices_[1]);
  350.     dispwid_ = (byte) strposch('|',choices_);
  351.     datawid = (byte) strposch('|',tempcdata);
  352.     strchcat(choices_,'|');
  353.     numc_ = (byte) (strlen(choices_)/(dispwid_+1));
  354.     whgt_ = (byte) minimum(scrhgt-2, numc_);
  355.     tlen = (byte) strlen(LSC_Choose) + 2;
  356.     wwid_ = (byte) ((dispwid_ < tlen) ? tlen : dispwid_);
  357.     x = (byte) (wherex()+datawid+1);
  358.     if ((x+wwid_+1) > scrwid) {
  359.         x = (byte) (wherex()-wwid_-1);
  360.         if (x <= 1)
  361.             x = (scrwid-wwid_)/2;
  362.     }
  363.  
  364.     y = (byte) wherey();
  365.     if ((y <= 1) || ((y+whgt_+1) > scrhgt))
  366.         y = (scrhgt-whgt_)/2;
  367.     if (openwin(0,x,y,wwid_,whgt_,swc,sbc,8,"─┐│┘─└│┌",TopCnt,LSC_Choose)) {
  368.         twinp_ = curwin;
  369.         twinp_->disp = True;
  370.         dispwindow(twinp_);
  371.         dispchoices(cnum_);
  372.         do {
  373.             if (numc_ != whgt_) {
  374.                 writewxy((line != cnum_) ? "\x18" : "\xC4",0,(byte)(x+3),(byte)(y+whgt_),NULL);
  375.                 writewxy(((numc_-cnum_)+line>whgt_) ? "\x19" : "\xC4",0,(byte)(x+4),(byte)(y+whgt_),NULL);
  376.             }
  377.             pad(ts,getchoice(ts,choices_,dispwid_,cnum_),wwid_,Right);
  378.             writewxy(ts,_av,1,line,twinp_);
  379.             cmd_ = getkey();
  380.             if ((cmd_ >= 'a') && (cmd_ <= 'z'))
  381.                 cmd_ -= 'a'-'A';
  382.             writewxy(ts,swc,1,line,twinp_);
  383.             switch (cmd_) {
  384.                 case HlpKey :
  385.                     if (choosehelp != NULL)
  386.                         choosehelp();
  387.                     break;
  388.                 case UArr   :
  389.                     if (cnum_ > 1) {
  390.                         cnum_--;
  391.                         if (line > 1)
  392.                             line--;
  393.                         else
  394.                             dispchoices(cnum_);
  395.                     }
  396.                     break;
  397.                 case DArr   :
  398.                     if (cnum_ < numc_) {
  399.                         cnum_++;
  400.                         if (line < whgt_)
  401.                             line++;
  402.                         else
  403.                             dispchoices((byte)(cnum_ - 2));
  404.                     }
  405.                     break;
  406.                 case Enter   :
  407.                 case XeptKey :
  408.                 case ' '     : break;
  409.                 default      : setchoice(cmd_);
  410.             }
  411.         } while ((cmd_ != Enter) && (cmd_ != XeptKey) && !(permitesc && (cmd_ == Esc)));
  412.         if (permitesc) exitcode = cmd_;
  413.         closewin(&twinp_);
  414.         if (cmd_ == Esc)
  415.             fld[0] = '\0';
  416.         else
  417.             getchoice(fld,tempcdata,datawid,cnum_);
  418.     }
  419.  
  420.     sout[0] = '\0';
  421.    return((*fld == '\0') ? 0 : cnum_);                                                    /* SN 3.5 */
  422. }
  423.  
  424. int selectfrom(byte line, string prompt, string clist, helpproc help)
  425. {
  426.     bool   tb;
  427.     int    fval,
  428.       l,
  429.       p;
  430.     string ts,
  431.       picked;
  432.     winptr twinp;
  433.    string tempclist;
  434.     byte   plen;
  435.  
  436.     fval = 0;
  437.     plen = (byte) strlen(prompt);
  438.     twinp = NULL;
  439.     if ((plen > 0) &&
  440.             openwin(0,(scrwid-plen)/2,line,plen,1,swc,sbc,8,"─┐│┘─└│┌",TopCnt,"")) {
  441.         twinp = curwin;
  442.         twinp->disp = True;
  443.         dispwindow(twinp);
  444.         writewxy(prompt,0,1,1,twinp);
  445.     }
  446.  
  447.  
  448.    padout(tempclist, clist);                                 /* SN 3.5 */
  449.  
  450.     l = strposch('|',tempclist)+1;
  451.     if (l >= 0) l = strlen(tempclist);
  452.     p = (scrwid-plen)/2 + plen - l;
  453.     gotoxy(p,line + 3);
  454.     tb = permitesc;
  455.     permitesc = True;
  456.     choosehelp = help;
  457.     fval = chooseExact(ts,tempclist,tempclist,picked);                 /* SN 3.5 */
  458.     permitesc = tb;
  459.     choosehelp = defChooseHelp;                                /* SN 3.5 */
  460.     if (twinp != NULL) closewin(&twinp);                       /* SN 3.5 */
  461.     return(fval);
  462. }
  463.  
  464. float txtinc(pathstr fileName, float fallBack)
  465. {
  466.    float   nextNumber;
  467.    FILE   *fileStream;
  468.    int     fileHandle;
  469.    long    filePosition;
  470.  
  471.    nextNumber = fallBack;
  472.    do {
  473.       fileHandle = sopen(fileName, O_RDWR | O_CREAT | O_BINARY, SH_DENYRW, S_IREAD | S_IWRITE);
  474.       if ((fileHandle == -1) && (errno == EACCES)) fileHandle = 0;
  475.    } while(fileHandle == 0) ;
  476.  
  477.    if (fileHandle > 0) {
  478.       fileStream = fdopen(fileHandle, "r+b");
  479.       if (!feof(fileStream)) {
  480.          fgetpos(fileStream, &filePosition);
  481.          if (fread(&nextNumber, sizeof(nextNumber), 1, fileStream) == 1)
  482.            nextNumber++;
  483.          fsetpos(fileStream, &filePosition);
  484.       }
  485.  
  486.       fwrite(&nextNumber, sizeof(nextNumber), 1, fileStream);
  487.       fclose(fileStream);
  488.    }
  489.    return(nextNumber);
  490. }
  491.  
  492. /* new for 3.6 an easy to use txtinc Chris */
  493.  
  494. char*  NewInc(char* fileName, unsigned long ulStart, unsigned int uPiclen)
  495. {
  496.    unsigned long  ulNextNumber;
  497.    FILE   *fileStream;
  498.    int     fileHandle;
  499.     long    filePosition;
  500.     /* make this static so we can return it*/
  501.     
  502.  
  503.     static char lpszValue[40];
  504.     /* set all positions to '\0' so it is always null terminated*/
  505.    memset (lpszValue,'\0',sizeof(lpszValue));
  506.  
  507.    ulNextNumber = ulStart;
  508.     do
  509.     {
  510.       fileHandle = sopen(fileName, O_RDWR | O_CREAT | O_BINARY, SH_DENYRW, S_IREAD | S_IWRITE);
  511.         if ((fileHandle == -1) && (errno == EACCES))
  512.             fileHandle = 0;
  513.    } while(fileHandle == 0) ;
  514.  
  515.     if (fileHandle > 0)
  516.     {
  517.       fileStream = fdopen(fileHandle, "r+b");
  518.         if (!feof(fileStream))
  519.         {
  520.          fgetpos(fileStream, &filePosition);
  521.          if (fread(&ulNextNumber, sizeof(ulNextNumber), 1, fileStream) == 1)
  522.               ulNextNumber++;
  523.          fsetpos(fileStream, &filePosition);
  524.       }
  525.  
  526.       fwrite(&ulNextNumber, sizeof(ulNextNumber), 1, fileStream);
  527.       fclose(fileStream);
  528.     }
  529.    /* convert the value back to a string */
  530.     ultoa(ulNextNumber,lpszValue,10);
  531.     /* make suer that it has th ecorrect number of digits */
  532.    lpszValue[uPiclen] = '\0';
  533.    return( lpszValue );
  534. }
  535.  
  536.  
  537.  
  538. void db_work_init(void)
  539. {
  540.     string ts;
  541.  
  542.     if (!initialized) {
  543.         initialized = True;
  544.         db_crit_init();
  545.         db_curs_init();
  546.         db_funcs_init();
  547.         db_gvar_init();
  548.         db_heap_init();
  549.         db_key_init();
  550.         db_win_init();
  551.         db_mnu_init();
  552.         permitesc = False;
  553.         choosehelp = defChooseHelp;
  554.     }
  555. }
  556.